package com.thssh.ui.lsn02.homework.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * 思路：
 * 问题1：怎么确定做上坐标？
 * 可以将每列高度记录下来，下一个子view添加在列高度最小的那一列。高度(Y坐标)就是该列的高，左边(X坐标)就是列总宽度平分的值。
 * 问题2：怎么知道那一列高度最小？
 * 暴力方法：遍历所有列进行比较，将最小列筛选出来
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/08
 * email: zhangyugehu@163.com
 */

public class WaterfallLayout extends ViewGroup {

    public static class WaterfallLayoutParams extends ViewGroup.LayoutParams{

        public int left = 0;
        public int top = 0;
        public int right = 0;
        public int bottom = 0;

        public WaterfallLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public WaterfallLayoutParams(int width, int height) {
            super(width, height);
        }

        public WaterfallLayoutParams(LayoutParams source) {
            super(source);
        }

        @Override
        public String toString() {
            return "l: " + left + " t: " + top + " r: " + right + " b: " + bottom;
        }
    }

    private int mColumns = 3;
    private int mHorizontalPadding = 20;
    private int mVerticalPadding = 20;

    private int[] mColumnBottom;

    public WaterfallLayout(Context context) {
        this(context, null);
    }

    public WaterfallLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaterfallLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new WaterfallLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mColumnBottom = new int[mColumns];

        int measuredWidth = 0;
        int measuredHeight = 0;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int suggestWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int suggestHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int averageRowWidth = suggestWidthSize / mColumns;

        int minColumnIndex = 0;

        int childCount = getChildCount();
        for(int i = 0; i < childCount; i++){
            View child = getChildAt(i);
            int childHeight = child.getMeasuredHeight();
            int childWidth = child.getMeasuredWidth();
            if(childWidth > averageRowWidth){
                int oldWidth = childWidth;
                childWidth = averageRowWidth;
                childHeight = (int) (childHeight * (childWidth * 1.0f / oldWidth * 1.0f));
            }

            WaterfallLayoutParams lp = (WaterfallLayoutParams) child.getLayoutParams();
            lp.left = minColumnIndex * (averageRowWidth + mHorizontalPadding);
            lp.right = lp.left + childWidth;
            lp.top = mColumnBottom[minColumnIndex];
            lp.bottom = lp.top + childHeight + mVerticalPadding;
            mColumnBottom[minColumnIndex] = lp.bottom;


            for(int j = 0; j < mColumns; j++){
                if(mColumnBottom[j] < mColumnBottom[minColumnIndex]){
                    minColumnIndex = j;
                }
            }
        }

        int maxColumnHeight = 0;
        for(int j = 0; j < mColumns; j++){
            if(mColumnBottom[j] > maxColumnHeight){
                maxColumnHeight = mColumnBottom[j];
            }
        }
        measuredWidth = suggestWidthSize;
        measuredHeight = maxColumnHeight;

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for(int i = 0; i < childCount; i++){
            View child = getChildAt(i);
            WaterfallLayoutParams layoutParams = (WaterfallLayoutParams) child.getLayoutParams();
            child.layout(layoutParams.left, layoutParams.top, layoutParams.right, layoutParams.bottom);
//            Log.d("WaterfallLayout", "child " + i + ": " + layoutParams.toString());
        }
    }
}
